
package com.devioustoast.turretdroid;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.TimeUnit;

import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.util.Log;

public class CameraProducer implements Runnable {

  private static final String LOG_TAG = CameraProducer.class.getSimpleName();
  private Camera mCamera;
  private BlockingDeque<byte[]> mPictureRoll;
  private boolean mRun = true;
  private PictureCallback mPictureCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
      if (data != null) {
        try {
          mPictureRoll.offer(data, 500, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
          Log.w(LOG_TAG, e);
        }
      }
    }};

  public CameraProducer(Camera camera, BlockingDeque<byte[]> pictureRoll) {
    mCamera = camera;
    mPictureRoll = pictureRoll;
  }

  public void run() {
    mRun = true;
    while (mRun) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        Log.w(LOG_TAG, e);
      }
      Log.d(LOG_TAG, "Taking picture.");
      mCamera.takePicture(null, null, mPictureCallback);
    }
  }

  public void stop() {
    mRun = false;
  }
}
